Skip to content

probe: fix J-Link swo_read crash when swo_num_bytes() returns a negative value - #2021

Open
adityaanikam wants to merge 2 commits into
pyocd:developfrom
adityaanikam:fix-jlink-swo-negative-count-2019
Open

probe: fix J-Link swo_read crash when swo_num_bytes() returns a negative value#2021
adityaanikam wants to merge 2 commits into
pyocd:developfrom
adityaanikam:fix-jlink-swo-negative-count-2019

Conversation

@adityaanikam

@adityaanikam adityaanikam commented Aug 17, 2026

Copy link
Copy Markdown

Fixes #2019.

JLinkProbe.swo_read() passed self._link.swo_num_bytes() straight into swo_read() as the count.
pylink allocates a ctypes buffer sized to that count, so passing 0 -- which is normal when no SWO
data is available -- crashes with IndexError, which isn't a JLinkException and so isn't caught by
the existing except clause either; it kills the SWV reader thread outright.

self._link.swo_num_bytes() never returns a negative value (pylink validates and raises internally
if the underlying call fails), so the guard only needs to check for zero, not negative -- thanks to
@RobertRostohar for catching that in review.

Guards the count before the call and returns an empty bytearray() when it's zero, matching
DebugProbe.swo_read's documented return type.

No test added, JLinkProbe's constructor calls into the real J-Link DLL and raises ProbeError if it's
not present, so testing this cleanly would mean bypassing normal construction in a way nothing else
in this codebase does.

…ive value

Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
@RobertRostohar

Copy link
Copy Markdown
Collaborator

Actually self._link.swo_num_bytes() does not retrun a negative value (already caught in pylink with error raised).

It can however return 0 which is normal when there is no data. When 0 is passed to pylink swo_read() it crashes with IndexError.

The required guard should only check for zero value.

Please update the code and commit message.

Comment thread pyocd/probe/jlink_probe.py Outdated
try:
return self._link.swo_read(0, self._link.swo_num_bytes(), True)
count = self._link.swo_num_bytes()
if count <= 0:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if count <= 0:
if count == 0:

@TeoMahnic
TeoMahnic changed the base branch from main to develop September 3, 2026 07:53
JLinkProbe.swo_read() passed self._link.swo_num_bytes() straight into
swo_read() as the count. pylink allocates a ctypes buffer sized to that
count; passing 0 (normal when there is no SWO data available) crashes
with IndexError, which isn't a JLinkException and so isn't caught by
the existing except clause -- it kills the SWV reader thread outright.

swo_num_bytes() itself never returns a negative value (pylink validates
and raises internally), so the guard only needs to cover zero. Return
an empty bytearray() in that case, matching DebugProbe.swo_read's
documented return type.

Fixes pyocd#2019.
@adityaanikam

adityaanikam commented Sep 3, 2026

Copy link
Copy Markdown
Author

Good catch, thanks , you're right that swo_num_bytes() can't go negative since pylink validates that internally. Updated the guard to count == 0 and rewrote the commit message/description to describe the zero case accurately instead of negative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

J-Link SWVReader crashes when swo_num_bytes() returns a negative value

2 participants